﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Interfaces;
using PrintManager;
using Helpers;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Controls;
using DataModel;
using System.Windows.Documents;

namespace UserCardPrinter
{
    [Export(typeof(IModule))]
    [ModuleMetadata(Name = "User cards", Short = "usercards", Group = "Labels", IconFilename = "UserCards.png")]
    public class UserCardPrinterViewModel : NotifyPropertyChangedBase, IModule
    {
        PrintEngine pe;
        public LayoutManager LayoutManager { get; set; }

        public UserCardPrinterViewModel()
        {
            var p = new Student {
                ID = 4711,
                FirstName = "John",
                LastName = "Doe"
            };
            pe = new PrintEngine();
            LayoutManager = new LayoutManager(pe, "usercards", p);
            
        }

 
        RelayCommand _printCommand;
        public ICommand PrintCommand
        {
            get
            {
                if (_printCommand == null)
                {
                    _printCommand = new RelayCommand(param => this.Print(),
                        param => this.CanPrint);
                }
                return _printCommand;
            }
        }

        public bool CanPrint { get { return true; } }

        public void Print()
        {
            DataLayer data = DataLayer.Data;
            var users = new List<object>(data.Persons);
            FixedDocument doc = pe.CreateDocument(users, LayoutManager.SelectedLayout, LayoutManager.SelectedPaper);
            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() != true)
                return;
            pd.PrintDocument(doc.DocumentPaginator, "");
        }

        public UserControl View()
        {
            var t = new UserCardPrinterView();
            t.DataContext = this;
            return t;
        }

    }
}
